package com.ruoyi.project.system.equipment.controller;

import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.file.domain.File;
import com.ruoyi.project.system.file.service.IFileService;
import com.ruoyi.project.system.trick.service.IVerbalTrickService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.equipment.domain.Equipment;
import com.ruoyi.project.system.equipment.service.IEquipmentService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author ruoyi
 * @date 2022-02-19
 */
@Controller
@RequestMapping("/system/equipment")
public class EquipmentController extends BaseController
{
    private String prefix = "system/equipment";

    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private IFileService fileService;
    @Autowired
    private IVerbalTrickService verbalTrickService;

//    @RequiresPermissions("system:equipment:view")
    @GetMapping()
    public String equipment()
    {
        return prefix + "/equipment";
    }

    /**
     * 查询设备列表
     */
//    @RequiresPermissions("system:equipment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Equipment equipment)
    {
        startPage();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        if (!loginUser.equals("admin")) {
            equipment.setUserName(loginUser);
        }
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
//    @RequiresPermissions("system:equipment:export")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Equipment equipment)
    {
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        return util.exportExcel(list, "设备数据");
    }

    /**
     * 新增设备
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 新增保存设备
     */
//    @RequiresPermissions("system:equipment:add")
    @Transactional
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Equipment equipment)
    {
        //获取输入的用户名
        String userName = equipment.getUserName();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        //获取输入的设备id
        Long equipmentId = equipment.getEquipmentId();
        //获取输入的微信号
        String wechatId = equipment.getWechatId();

        //根据设备id和用户名查询
        List<Equipment> equipmentList = equipmentService.selectEquipmentByEquipmentIdAndUserName(equipmentId, loginUser);
        //根据微信号查询
        List<Equipment> equipmentListWechatId = equipmentService.selectEquipmentByWechatId(wechatId);
        if (!equipmentList.isEmpty() || equipmentId==null){
            return AjaxResult.error("设备已存在或为空在请重新输入！");
        }
        if (!equipmentListWechatId.isEmpty() || wechatId.isEmpty()){
            return AjaxResult.error("微信号已存在或为空在请重新输入！");
        }
        if (userName.equals(loginUser)){
            equipmentService.insertEquipment(equipment);
        }else {
            return AjaxResult.error("请输入本人的登录名称!");
        }
        if (userName.equals(loginUser)){
            verbalTrickService.insertEquipmentIdAndUserNameAndWechatId(equipmentId,userName,wechatId);
        }else {
            return AjaxResult.error("请输入本人的登录名称!");
        }
        if (userName.equals(loginUser)){
            return toAjax(fileService.insertEquipmentIdAndUserName(equipmentId,userName));
        }
        else{
            return AjaxResult.error("请输入本人的登录名称!");
        }
    }


    /**
     * 修改设备
     */
//    @RequiresPermissions("system:equipment:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Equipment equipment = equipmentService.selectEquipmentById(id);
        mmap.put("equipment", equipment);
        return prefix + "/edit";
    }


    /**
     * 修改保存设备
     */
//    @RequiresPermissions("system:equipment:edit")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Equipment equipment)
    {
        //获取输入的用户名
        String userName = equipment.getUserName();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        //获取输入的设备id
        Long equipmentId = equipment.getEquipmentId();
        //获取输入的微信号
        String wechatId = equipment.getWechatId();
        //根据设备id查询
        List<Equipment> equipmentListEquipmentId = equipmentService.selectEquipmentByEquipmentIdAndUserName(equipmentId,loginUser);
        //根据微信号查询
        List<Equipment> equipmentListWechatId = equipmentService.selectEquipmentByWechatId(wechatId);
        if (!equipmentListEquipmentId.isEmpty() || equipmentId==null){
            return AjaxResult.error("设备已存在或为空在请重新输入！");
        }
        if (!equipmentListWechatId.isEmpty() || wechatId.isEmpty()){
            return AjaxResult.error("微信号已存在或为空在请重新输入！");
        }
        if (userName.equals(loginUser)){
            return toAjax(equipmentService.updateEquipment(equipment));
        }else {
            return AjaxResult.error("请输入本人的登录名称!");
        }
    }



    /**
     * 删除设备
     */
//    @RequiresPermissions("system:equipment:remove")
    @Transactional
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Long ids)
    {
        Equipment equipment = equipmentService.selectEquipmentById(ids);
        Long equipmentId = equipment.getEquipmentId();
        String userName = equipment.getUserName();

        equipmentService.deleteEquipmentById(ids);
        verbalTrickService.deleteFileByEquipmentIdAndUserName(equipmentId,userName);
        return toAjax(fileService.deleteFileByEquipmentIdAndUserName(equipmentId,userName));
    }
}
